﻿/// <summary>
/// jQuery Utility Plugin
/// author:hongda.yu@jrj.com.cn
///</summary>
(function ($) {
    $.utility = {

        //ellipsis
        ellipsis: function (params) {
            
            var settings = $.extend({
                selector: '.ellipsis',
                length: 20,
                char: '...'
            }, params);

            $(settings.selector).each(function (i, v) {
                var text = $(this).text(),
                    length = text.length;
                if (length > settings.length) {
                    $(this).html(text.substring(0, settings.length) + settings.char);
                }
            });
        },
        //encode & decode
        encode: function (input) {
            return $("<div/>").text(input).html();
        },
        decode: function (input) {
            return $("<div/>").html(input).text();
        },

        //get url parameter
        getPara: function request(paras) {
            var url = location.href;
            var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
            var paraObj = {}
            for (i = 0; j = paraString[i]; i++) {
                paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
            }
            var returnValue = paraObj[paras.toLowerCase()];
            if (typeof (returnValue) == "undefined") {
                return "";
            } else {
                return returnValue;
            }
        },

        //alert dialog
        alert: function (params) {
            var settings = $.extend({
                div: '.popup_alert',
                parent: '#wrapper',
                btnok: '.btnok',
                btnclose: '.close',
                width: 340,
                top: null
            }, params);

            $(settings.div)
                .css({ width: settings.width + "px" })
                .css({
                    top: (!settings.top ? $(settings.parent).height() / 2 - $(settings.div).height() / 2 : settings.top + "px"),
                    left: $(settings.parent).width() / 2 - $(settings.div).width() / 2 + ($(window).width() - $(settings.parent).width()) / 2
                }).show();
            $(settings.btnok).click(function () {
                $(settings.div).hide();
            });
            $(settings.btnclose).click(function () {
                $(settings.div).hide();
            });
        },

        //accordion
        accordion: function (params) {
            var settings = $.extend({
                wrapper: '.accordion'
            }, params);

            function _init() {
                $(settings.wrapper).find('.expand').hide();
                $(settings.wrapper).find('.expand:first').show();
                $(settings.wrapper).find('.item:first').hide();
            }

            _init();

            $(settings.wrapper).find('.item').mouseenter(function () {
                $(settings.wrapper).find('.item').show();
                $(settings.wrapper).find('.expand').hide();
                $(this).hide();
                $(this).next('.expand').show();
            });

            $(settings.wrapper).mouseleave(function () {
            });
        }
    }
})(jQuery);